<template>
	<view>
		<uni-data-menu :localdata="menus" :unique-opened="true"  v-model="activeUrl" active-text-color="#409eff"
			@select="select"></uni-data-menu>
	</view>
</template>

<script>
	import config from '@/admin.config.js';
	import {
	  getByMenu, 
	} from '@/api/menu.js'
	import {
		hikSingleToken
	} from '@/api/workers.js'
	export default {
		data() {
			return {
				activeUrl: '/pages/index/index',
				menus: [],
				token: '',
				countUrl: ''
			}
		},
		watch: {
			$route: {
				immediate: true,
				handler(newRoute, oldRoute) {
					console.log('当前登录页',newRoute, oldRoute)
					if(newRoute.path=='/pages/index/index'){
						this.getByMenu()
					}
					
					const path = newRoute.fullPath
					console.log('pathssssssssss',path)
					if (path) {
					
						this.activeUrl=path}
					
				}
			},
			userInfo: {
				// immediate: true,
				handler(newVal, oldVal) {
					if (newVal) {
						// 当用户信息发生变化后，重新加载左侧menu
						this.$nextTick(function() {
							this.$refs.menu.load()
						})
					}
				}
			}
		},
		onShow(){
			
		},
		methods: {
			async gettoken() {
				const {
					data
				} = await hikSingleToken({
					// userName:this.$util.getUser().loginName,
					userName: 'admin',
					url: 'https://alt.xjtgzh.com:8004/portal/ui/index?menuId=000198'
				})
					this.token = JSON.parse(data.data).data.token
					this.countUrl =
						'https://alt.xjtgzh.com:8004/bic/ssoService/v1/tokenLogin?service=https://alt.xjtgzh.com:8004/portal/ui/index?menuId=000198&token=' +
						this.token;
					console.log('dddddssss',this.countUrl)
					this.openBaidu(this.countUrl)
			},
			openBaidu(url) {
				window.open(url, '_blank');
			},
			
			async getByMenu(){
				const {data} =await getByMenu()
				if(data.data){
					this.newMenu=JSON.stringify(data.data)
					this.newMenu=this.newMenu.replace(/id/gi,'menu_id');
					this.newMenu=this.newMenu.replace(/name/gi,'text');
					this.newMenu=this.newMenu.replace(/jump/gi,'value');
					this.menus=JSON.parse(this.newMenu)
				}
				
				
			},
			
			treeFindPath(tree, func, path = []) {
			    if (!tree) return []
			    for (const data of tree) {
			     path.push(data.id)
			     if (func(data)) return path
			     if (data.children) {
			      const findChildren = treeFindPath(data.children, func, path)
			      if (findChildren.length) return findChildren
			     }
			     path.pop()
			    }
			    return []
			   },
			select(e) {
				const value = e.value
				console.log('----select value:', value)
				if(value=='/systemsubPack/attendance/staff'){
					this.gettoken()
				}else{
					uni.redirectTo({
						url:value
					})
				}
				
			},
			splitFullPath(path) {
				if (!path) {
					path = '/'
				}
				return path.split('?')[0]
			},
		}
	}
</script>
